package com.emperor.misc.app;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

import com.emperor.cashier.app.CashierAppCtrl;
import com.emperor.category.app.CategoryAppCtrl;
import com.emperor.core.app.AppConstant;
import com.emperor.core.app.AppCtrl;
import com.emperor.course.app.CourseAppCtrl;
import com.emperor.customer.app.CustomerAppCtrl;
import com.emperor.employee.app.EmployeeAppCtrl;
import com.emperor.employee.logic.Employee;
import com.emperor.inventory.app.InventoryAppCtrl;
import com.emperor.product.app.ProductAppCtrl;
import com.emperor.receipt.app.ReceiptAppCtrl;
import com.emperor.report.app.ReportAppCtrl;
import com.emperor.treatment.app.TreatmentAppCtrl;
import com.emperor.user.app.UserAppCtrl;
import com.emperor.util.HibernateUtil;
import com.emperor.work.app.WorkAppCtrl;

public class StartupAppCtrl {

	public static Logger log = Logger.getLogger(StartupAppCtrl.class);

	private static void doShutdown() {
		HibernateUtil.getSessionFactory().close();
		log.info("doShutdown");
	}

	public static void main(String[] args) {
		Map<String, Object> params = new HashMap<String, Object>();
		Boolean Online = new Boolean(true);
		int ctrlMissing = 0;

		DOMConfigurator.configure(AppConstant.LOG4J_CONFIG);

		while (Online.booleanValue() && ctrlMissing < 10) {
			AppCtrl ctrl = null;
			if (params == null) {
				log.error("Param is null");
			} else if (params.get(AppConstant.APPCTRL) == null) {
				// initialize system
				params.put(AppConstant.ONLINE, Online);
				params.put(AppConstant.ACTION, LoginAppCtrl.LOGIN_FORM);

				ctrl = new LoginAppCtrl();
			} else if (params.get(AppConstant.APPCTRL).equals(
					LoginAppCtrl.LOGIN_CTRL)) {
				ctrl = new LoginAppCtrl();
			} else if (params.get(AppConstant.APPCTRL).equals(
					CashierAppCtrl.CASHIER_CTRL)) {
				ctrl = new CashierAppCtrl();
			} else if (params.get(AppConstant.APPCTRL).equals(
					InventoryAppCtrl.INVENTORY_CTRL)) {
				ctrl = new InventoryAppCtrl();
			} else if (params.get(AppConstant.APPCTRL).equals(
					CustomerAppCtrl.CUST_CTRL)) {
				ctrl = new CustomerAppCtrl();
			} else if (params.get(AppConstant.APPCTRL).equals(
					EmployeeAppCtrl.EMP_CTRL)) {
				ctrl = new EmployeeAppCtrl();
			} else if (params.get(AppConstant.APPCTRL).equals(
					ProductAppCtrl.PRODUCT_CTRL)) {
				ctrl = new ProductAppCtrl();
			} else if (params.get(AppConstant.APPCTRL).equals(CategoryAppCtrl.CATEGORY_CTRL)) {
				ctrl = new CategoryAppCtrl();
			} else if (params.get(AppConstant.APPCTRL).equals(WorkAppCtrl.WORK_CTRL)) {
				ctrl = new WorkAppCtrl();
			} else if (params.get(AppConstant.APPCTRL).equals(ReportAppCtrl.REPORT_CTRL)) {
				ctrl = new ReportAppCtrl();
			} else if (params.get(AppConstant.APPCTRL).equals(HelpAppCtrl.HELP_CTRL)) {
				ctrl = new HelpAppCtrl();
			} else if (params.get(AppConstant.APPCTRL).equals(ReceiptAppCtrl.RECEIPT_CTRL)) {
				ctrl = new ReceiptAppCtrl();
			} else if (params.get(AppConstant.APPCTRL).equals(CourseAppCtrl.COURSE_CTRL)) {
				ctrl = new CourseAppCtrl();
			} else if (params.get(AppConstant.APPCTRL).equals(TreatmentAppCtrl.TREATMENT_CTRL)) {
				ctrl = new TreatmentAppCtrl();
			}
			else {
				log.error("CTRL is unknown or missing :"
						+ params.get(AppConstant.APPCTRL));
				ctrlMissing++;
			}

			if (ctrl != null) {
				ctrl.doProcess(params);
			}

			if (params != null && params.get(AppConstant.ONLINE) != null) {
				Online = (Boolean) params.get(AppConstant.ONLINE);
			}
		}
		doShutdown();
	}
}
